package com.haochang.chunkvod.controller.adapter.order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.activity.order.OrderChangeActivity;
import com.haochang.chunkvod.controller.fragment.order.AlreadyOrderFragment;
import com.haochang.chunkvod.model.order.FoodDetailInfo;
import com.haochang.chunkvod.model.order.FoodInfo;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.model.order.query.OrderInfo;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;
import com.haochang.chunkvod.tools.vod.other.AppendImageUtil;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.view.vod.activity.AuthorizationDialogActivity;
import com.haochang.chunkvod.view.vod.activity.CustomDialogActivity;
import com.haochang.chunkvod.view.vod.popupWindow.IPopWindowLongClickListener;
import com.haochang.chunkvod.view.vod.spinner.OperateView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * @author qintao
 *         created at 2016/6/29 19:08
 *         点餐查询适配器
 */

public class OrderQueryAdapter extends BaseAdapter implements IPopWindowLongClickListener
{
	private AlreadyOrderFragment alreadyOrderFragment;
	private Context context;
	private String roomId;
	private String billId;
	private String charging_room_type_id;
	private List<OrderInfo> infoList = new ArrayList<OrderInfo>();
	private GoodsInfo goodsInfo = new GoodsInfo();
	private OperateView currentView = null;
	private ListView listView;

	public OrderQueryAdapter(AlreadyOrderFragment alreadyOrderFragment, Context context, String roomId, String billId, String charging_room_type_id,
			ListView listView)
	{
		this.alreadyOrderFragment = alreadyOrderFragment;
		this.context = context;
		this.roomId = roomId;
		this.billId = billId;
		this.charging_room_type_id = charging_room_type_id;
		this.listView = listView;
	}

	public void setData(List<OrderInfo> list)
	{
		if (!CollectionUtils.isEmpty(list))
		{
			infoList.clear();
			infoList.addAll(list);
			parseGoodInfo();
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount()
	{
		return CollectionUtils.isEmpty(infoList) ? 0 : infoList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return CollectionUtils.isEmpty(infoList) ? null : infoList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null;
		if (null == convertView)
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.item_order_query_layout, parent, false);
			holder = new ViewHolder();
			holder.tv_title = (BaseTextView) convertView.findViewById(R.id.tv_title);
			holder.tv_time = (BaseTextView) convertView.findViewById(R.id.tv_time);
			holder.lay_content_div = (LinearLayout) convertView.findViewById(R.id.lay_content_div);
			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();
		holder.lay_content_div.removeAllViews();
		OrderInfo orderInfo = infoList.get(position);
		final String flow_code = orderInfo.getFlow_code();
		holder.tv_title.setText(orderInfo.getBill_channel());
		holder.tv_time.setText(orderInfo.getFlow_time());
		List<GoodsInfo> infoList = orderInfo.getGoodsInfoList();
		if (!CollectionUtils.isEmpty(infoList))
		{
			for (int i = 0; i < infoList.size(); i++)
			{
				final GoodsInfo info = infoList.get(i);
				View secondView = LayoutInflater.from(context).inflate(R.layout.item_order_query_second_layout, null, false);
				BaseTextView tv_foodName = (BaseTextView) secondView.findViewById(R.id.tv_foodName);
				BaseTextView tv_num_unit = (BaseTextView) secondView.findViewById(R.id.tv_num_unit);
				BaseTextView tv_totalPrice = (BaseTextView) secondView.findViewById(R.id.tv_totalPrice);
				BaseTextView tv_out_single = (BaseTextView) secondView.findViewById(R.id.tv_out_single);
				final RelativeLayout title_div = (RelativeLayout) secondView.findViewById(R.id.title_div);
				final OperateView operateView = (OperateView) secondView.findViewById(R.id.operateView);
				final ImageView iv_direction = (ImageView) secondView.findViewById(R.id.iv_direction);
				View v_line = secondView.findViewById(R.id.v_line);
				if (i == 0)
					v_line.setVisibility(View.INVISIBLE);
				else
					v_line.setVisibility(View.VISIBLE);
				if (info.is_back())
				{
					tv_num_unit.setVisibility(View.VISIBLE);
					tv_foodName.setText(Html.fromHtml("<font color=#f04e39>" + info.getGoodsName() + "</font>"));
					tv_num_unit.setText(Html.fromHtml("<font color=#f04e39>" + info.getBackNum() + info.getUnit() + "" + "</font>"));
					tv_totalPrice.setText(Html.fromHtml("<font color=#f04e39>" + SaleUtils.getSaleString(info.getPrice() * info.getBackNum()) + "</font>"));
					tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
					tv_out_single.setVisibility(View.INVISIBLE);
					iv_direction.setVisibility(View.GONE);
				} else
				{
					if (TextUtils.equals(info.getGoodsId(), goodsInfo.getGoodsId()) && info.getD_id_value().equals(goodsInfo.getD_id_value())
							&& goodsInfo.isShowOperate() && TextUtils.equals(info.getParentDIdValue(), goodsInfo.getParentDIdValue()))
						showGoodsOperateStyle(operateView, info, flow_code, info.getParentDIdValue(), false);
					else
						operateView.setVisibility(View.GONE);
					tv_foodName.setText(info.getGoodsName());
					if (info.is_give())
					{
						tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
						tv_totalPrice
								.setText(Html.fromHtml("<font color=#f04e39>" + SaleUtils.getSaleString(info.getPrice() * info.getOrderNum()) + "</font>"));
						tv_totalPrice.getPaint().setAntiAlias(true);//抗锯齿
						tv_totalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
						if (!TextUtils.isEmpty(info.getBill_promotion_id()))//优惠活动赠送
							tv_foodName.append(AppendImageUtil.addImageSpan(context, R.drawable.is_active_gift, 10));
						else
							tv_foodName.append(AppendImageUtil.addImageSpan(context, R.drawable.btn, 10));
					} else
					{
						tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_gray), null, null, null);
						tv_totalPrice.setText(SaleUtils.getSaleString(info.getPrice() * info.getOrderNum()));
					}
					tv_num_unit.setText(String.valueOf(info.getOrderNum() + info.getUnit()));
					tv_num_unit.setVisibility(View.VISIBLE);
					tv_out_single.setText(info.getOutput_status() == 2 ? "已划单" : Html.fromHtml("<font size=6 color=#ffab40>" + "下单" + "</font>"));
					tv_out_single.setVisibility(View.VISIBLE);
					final LinearLayout lay_content_div = (LinearLayout) secondView.findViewById(R.id.lay_content_div);
					lay_content_div.removeAllViews();
					if (info.isExpand())
					{
						lay_content_div.setVisibility(View.VISIBLE);
						iv_direction.setImageResource(R.drawable.up);
					} else
					{
						lay_content_div.setVisibility(View.GONE);
						iv_direction.setImageResource(R.drawable.down);
					}
					if (info.getGoods_type() == VodConstant.BUNDLE_TYPE)
					{
						tv_out_single.setVisibility(View.INVISIBLE);
						List<FoodDetailInfo> retails = info.getFoodDetailInfoList();
						if (!CollectionUtils.isEmpty(retails))
						{
							iv_direction.setVisibility(View.VISIBLE);
							for (int j = 0; j < retails.size(); j++)
							{
								final FoodDetailInfo foodDetailInfo = retails.get(j);
								final View fourView = LayoutInflater.from(context).inflate(R.layout.item_order_query_four_layout, null, false);
								BaseTextView tv_four_foodName = (BaseTextView) fourView.findViewById(R.id.tv_foodName);
								BaseTextView tv_four_num_unit = (BaseTextView) fourView.findViewById(R.id.tv_num_unit);
								BaseTextView tv_four_out_single = (BaseTextView) fourView.findViewById(R.id.tv_out_single);
								final OperateView four_operateView = (OperateView) fourView.findViewById(R.id.four_operateView);
								View spaceLineFour = fourView.findViewById(R.id.spaceLineFour);
								RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) spaceLineFour.getLayoutParams();
								layoutParams.setMargins(DipPxConversion.dip2px(context, j == 0 ? 16 : 48), 0, 0, 0);
								spaceLineFour.setLayoutParams(layoutParams);
								tv_four_foodName.setText(foodDetailInfo.getName());
								tv_four_num_unit.setText(String.valueOf(foodDetailInfo.getQuantity() + foodDetailInfo.getUnit()));
								tv_four_out_single.setText(
										foodDetailInfo.getOutput_status() == 2 ? "已划单" : Html.fromHtml("<font size=6 color=#ffab40>" + "下单" + "</font>"));
								lay_content_div.addView(fourView);
								if (TextUtils.equals(foodDetailInfo.getFood_id(), goodsInfo.getGoodsId())
										&& foodDetailInfo.getD_id_value().equals(goodsInfo.getD_id_value()) && goodsInfo.isShowOperate()
										&& TextUtils.equals(info.getParentDIdValue(), goodsInfo.getParentDIdValue()))
									showFoodDetailOperateStyle(four_operateView, foodDetailInfo, flow_code, info.getIs_outposition(), true, info.is_give(),
											isChangeGoods(info, flow_code), info.getD_id_value(), false);
								else
									four_operateView.setVisibility(View.GONE);
								//属于套餐的商品
								fourView.setOnClickListener(new OnBaseClickListener()
								{
									@Override
									public void onBaseClick(View v)
									{
										showFoodDetailOperateStyle(four_operateView, foodDetailInfo, flow_code, info.getIs_outposition(), true, info.is_give(),
												isChangeGoods(info, flow_code), info.getD_id_value(), true);
										if (listView.getLastVisiblePosition() == position)
											listView.setSelection(listView.getFirstVisiblePosition() + 1);
									}
								});
							}
						}
					} else if (info.getGoods_type() == VodConstant.ACTIVE_TYPE)
					{
						tv_num_unit.setVisibility(View.GONE);
						tv_out_single.setVisibility(View.INVISIBLE);
						iv_direction.setVisibility(View.VISIBLE);
						//活动里面的单品
						int free_time = info.getDecrease_time();
						if (free_time > 0)
						{
							View threeView = LayoutInflater.from(context).inflate(R.layout.item_order_query_active_layout, null, false);
							BaseTextView tv_three_foodName = (BaseTextView) threeView.findViewById(R.id.tv_foodName);
							tv_three_foodName.setText(String.valueOf("减免" + free_time + "分钟房费"));
							lay_content_div.addView(threeView);
						}
						List<FoodDetailInfo> retails = info.getFoodDetailInfoList();
						if (!CollectionUtils.isEmpty(retails))
						{
							for (int j = 0; j < retails.size(); j++)
							{
								final FoodDetailInfo foodInfo = retails.get(j);
								View threeView = LayoutInflater.from(context).inflate(R.layout.item_order_query_three_layout, null, false);
								BaseTextView tv_three_foodName = (BaseTextView) threeView.findViewById(R.id.tv_foodName);
								BaseTextView tv_three_num_unit = (BaseTextView) threeView.findViewById(R.id.tv_num_unit);
								BaseTextView tv_three_out_single = (BaseTextView) threeView.findViewById(R.id.tv_out_single);
								final LinearLayout lay_next_content_div = (LinearLayout) threeView.findViewById(R.id.lay_content_div);
								final OperateView three_operateView = (OperateView) threeView.findViewById(R.id.three_operateView);
								View spaceLineThree = threeView.findViewById(R.id.spaceLineThree);
								RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) spaceLineThree.getLayoutParams();
								layoutParams.setMargins(DipPxConversion.dip2px(context, j == 0 ? 16 : 32), 0, 0, 0);
								spaceLineThree.setLayoutParams(layoutParams);
								tv_three_foodName.setText(foodInfo.getName());
								tv_three_num_unit.setText(String.valueOf(foodInfo.getQuantity() + foodInfo.getUnit()));
								tv_three_out_single
										.setText(foodInfo.getOutput_status() == 2 ? "已划单" : Html.fromHtml("<font size=6 color=#ffab40>" + "下单" + "</font>"));
								lay_next_content_div.setVisibility(View.VISIBLE);
								if (TextUtils.equals(foodInfo.getFood_id(), goodsInfo.getGoodsId())
										&& foodInfo.getD_id_value().equals(goodsInfo.getD_id_value()) && goodsInfo.isShowOperate()
										&& TextUtils.equals(info.getParentDIdValue(), goodsInfo.getParentDIdValue()))
									showFoodDetailOperateStyle(three_operateView, foodInfo, flow_code, info.getIs_outposition(), false, info.is_give(),
											isChangeGoods(info, flow_code), info.getD_id_value(), false);
								else
									three_operateView.setVisibility(View.GONE);
								threeView.setOnClickListener(new OnBaseClickListener()
								{
									@Override
									public void onBaseClick(View v)
									{
										//活动里面的单品
										showFoodDetailOperateStyle(three_operateView, foodInfo, flow_code, info.getIs_outposition(), false, info.is_give(),
												isChangeGoods(info, flow_code), info.getD_id_value(), true);
										if (listView.getLastVisiblePosition() == position)
											listView.setSelection(listView.getFirstVisiblePosition() + 1);
									}
								});
								lay_content_div.addView(threeView);
							}
						}
						//活动里的套餐
						List<FoodInfo> bundles = info.getFoodInfoList();
						if (!CollectionUtils.isEmpty(bundles))
						{
							for (int j = 0; j < bundles.size(); j++)
							{
								final FoodInfo foodInfo = bundles.get(j);
								View threeView = LayoutInflater.from(context).inflate(R.layout.item_order_query_three_layout, null, false);
								BaseTextView tv_three_foodName = (BaseTextView) threeView.findViewById(R.id.tv_foodName);
								BaseTextView tv_three_num_unit = (BaseTextView) threeView.findViewById(R.id.tv_num_unit);
								BaseTextView tv_three_out_single = (BaseTextView) threeView.findViewById(R.id.tv_out_single);
								final LinearLayout lay_next_content_div = (LinearLayout) threeView.findViewById(R.id.lay_content_div);
								final ImageView iv_three_direction = (ImageView) threeView.findViewById(R.id.iv_direction);
								final OperateView three_operateView = (OperateView) threeView.findViewById(R.id.three_operateView);
								View spaceLineThree = threeView.findViewById(R.id.spaceLineThree);
								RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) spaceLineThree.getLayoutParams();
								layoutParams.setMargins(DipPxConversion.dip2px(context, 32), 0, 0, 0);
								spaceLineThree.setLayoutParams(layoutParams);
								tv_three_foodName.setText(foodInfo.getName());
								tv_three_num_unit.setText(String.valueOf(foodInfo.getQuantity() + foodInfo.getUnit()));
								tv_three_out_single.setVisibility(View.INVISIBLE);
								lay_next_content_div.removeAllViews();
								if (foodInfo.isExpand())
								{
									lay_next_content_div.setVisibility(View.VISIBLE);
									iv_three_direction.setImageResource(R.drawable.up);
								} else
								{
									iv_three_direction.setImageResource(R.drawable.down);
									lay_next_content_div.setVisibility(View.GONE);
								}
								List<FoodDetailInfo> retailsDetail = foodInfo.getFoodList();
								iv_three_direction.setOnClickListener(new View.OnClickListener()
								{
									@Override
									public void onClick(View v)
									{
										if (lay_next_content_div.getVisibility() == View.VISIBLE)
										{
											lay_next_content_div.setVisibility(View.GONE);
											iv_three_direction.setImageResource(R.drawable.down);
											foodInfo.setExpand(false);
										} else
										{
											lay_next_content_div.setVisibility(View.VISIBLE);
											iv_three_direction.setImageResource(R.drawable.up);
											foodInfo.setExpand(true);
										}
									}
								});
								if (!CollectionUtils.isEmpty(retailsDetail))
								{
									iv_three_direction.setVisibility(View.VISIBLE);
									for (int k = 0; k < retailsDetail.size(); k++)
									{
										final FoodDetailInfo foodDetailInfo = retailsDetail.get(k);
										View fourView = LayoutInflater.from(context).inflate(R.layout.item_order_query_four_layout, null, false);
										BaseTextView tv_four_foodName = (BaseTextView) fourView.findViewById(R.id.tv_foodName);
										BaseTextView tv_four_num_unit = (BaseTextView) fourView.findViewById(R.id.tv_num_unit);
										BaseTextView tv_four_out_single = (BaseTextView) fourView.findViewById(R.id.tv_out_single);
										final OperateView four_operateView = (OperateView) fourView.findViewById(R.id.four_operateView);
										View spaceLineFour = fourView.findViewById(R.id.spaceLineFour);
										RelativeLayout.LayoutParams layoutParams4 = (RelativeLayout.LayoutParams) spaceLineFour.getLayoutParams();
										layoutParams4.setMargins(DipPxConversion.dip2px(context, k == 0 ? 32 : 48), 0, 0, 0);
										spaceLineFour.setLayoutParams(layoutParams4);
										tv_four_foodName.setText(foodDetailInfo.getName());
										tv_four_num_unit.setText(String.valueOf(foodDetailInfo.getQuantity() + foodDetailInfo.getUnit()));
										tv_four_out_single.setText(foodDetailInfo.getOutput_status() == 2 ? "已划单"
												: Html.fromHtml("<font size=6 color=#ffab40>" + "下单" + "</font>"));
										if (TextUtils.equals(foodDetailInfo.getFood_id(), goodsInfo.getGoodsId())
												&& foodDetailInfo.getD_id_value().equals(goodsInfo.getD_id_value()) && goodsInfo.isShowOperate()
												&& TextUtils.equals(info.getParentDIdValue(), goodsInfo.getParentDIdValue()))
											showFoodDetailOperateStyle(four_operateView, foodDetailInfo, flow_code, foodInfo.getIs_outposition(), true,
													info.is_give(), isChangeGoods(info, flow_code), info.getD_id_value(), false);
										else
											four_operateView.setVisibility(View.GONE);
										fourView.setOnClickListener(new OnBaseClickListener()
										{
											@Override
											public void onBaseClick(View v)
											{
												showFoodDetailOperateStyle(four_operateView, foodDetailInfo, flow_code, foodInfo.getIs_outposition(), true,
														info.is_give(), isChangeGoods(info, flow_code), info.getD_id_value(), true);
												if (listView.getLastVisiblePosition() == position)
													listView.setSelection(listView.getFirstVisiblePosition() + 1);
											}
										});
										lay_next_content_div.addView(fourView);
									}
								}
								lay_content_div.addView(threeView);
							}
						}
					} else
					{
						iv_direction.setVisibility(View.GONE);
					}
					iv_direction.setOnClickListener(new View.OnClickListener()
					{

						@Override
						public void onClick(View v)
						{
							if (lay_content_div.getVisibility() == View.VISIBLE)
							{
								lay_content_div.setVisibility(View.GONE);
								iv_direction.setImageResource(R.drawable.down);
								info.setExpand(false);
							} else
							{
								lay_content_div.setVisibility(View.VISIBLE);
								iv_direction.setImageResource(R.drawable.up);
								info.setExpand(true);
							}
						}
					});
					title_div.setOnClickListener(new View.OnClickListener()
					{

						@Override
						public void onClick(View v)
						{
							showGoodsOperateStyle(operateView, info, flow_code, info.getD_id_value(), true);
							if (listView.getLastVisiblePosition() == position)
								listView.setSelection(listView.getFirstVisiblePosition() + 1);
						}
					});
				}
				holder.lay_content_div.addView(secondView);
			}
		}
		return convertView;
	}

	/**
	 * 展示套餐或者活动详情里面单品样式
	 *
	 * @param operateView   操作行view
	 * @param info          商品信息
	 * @param flow_code     流水号
	 * @param isOutProperty 是否有出餐点属性
	 * @param isPack        是否属于套餐
	 * @param isParentGive  活动或者套餐是否为增送
	 * @param isChange      是否更换过商品
	 */
	private void showFoodDetailOperateStyle(OperateView operateView, FoodDetailInfo info, String flow_code, int isOutProperty, boolean isPack,
			boolean isParentGive, boolean isChange, String parentDidValue, boolean isRefreshData)
	{
		//属于活动或者套餐的单品
		if (operateView.getVisibility() == View.GONE)
		{
			currentView = operateView;
			goodsInfo.setD_id_value(info.getD_id_value());
			goodsInfo.setGoodsId(info.getFood_id());
			goodsInfo.setShowOperate(true);
			goodsInfo.setParentDIdValue(parentDidValue);
			operateView.setVisibility(View.VISIBLE);
			if (isParentGive)//活动或者套餐是赠送
			{
				if (!isPack)
					operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
				else
				{
					if (isOutProperty == 0)
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
				}
			} else //活动或者套餐不是赠送
			{
				if (isChange)
				{
					if (!isPack)
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
					else
					{
						if (isOutProperty == 0)
							operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
					}
				} else
				{
					if (!isPack)
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);//更换按钮隐藏
					//	operateView.initViewData(OperateView.DialogEnum.DOUBLE_REMINDER_CHANGE, flow_code, info);
					else
					{
						if (isOutProperty == 1)
							System.out.println("无操作");
						//	operateView.initViewData(OperateView.DialogEnum.SINGLE_CHANGE, flow_code, info);
						else
							operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);//更换按钮隐藏
						//	operateView.initViewData(OperateView.DialogEnum.DOUBLE_REMINDER_CHANGE, flow_code, info);
					}
				}
			}
			operateView.setPopWindowLongClickListener(OrderQueryAdapter.this);
			if (isRefreshData)
				notifyDataSetChanged();
		} else
		{
			restoreCurrentView(operateView);
		}
	}

	/**
	 * 展示商品样式
	 *
	 * @param operateView 操作行view
	 * @param info        商品信息
	 * @param flow_code   流水号
	 */
	private void showGoodsOperateStyle(OperateView operateView, GoodsInfo info, String flow_code, String parentDidValue, boolean isRefreshData)
	{
		if (info.getGoods_type() == VodConstant.RETAIL_TYPE)
		{ //不属于活动或者套餐的单品
			if (operateView.getVisibility() == View.GONE)
			{
				currentView = operateView;
				goodsInfo.setD_id_value(info.getD_id_value());
				goodsInfo.setGoodsId(info.getGoodsId());
				goodsInfo.setShowOperate(true);
				goodsInfo.setParentDIdValue(parentDidValue);
				operateView.setVisibility(View.VISIBLE);
				if (info.is_give())
				{
					if (!TextUtils.isEmpty(info.getBill_promotion_id()))//优惠活动赠送
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
					else
						operateView.initViewData(OperateView.DialogEnum.DOUBLE_REMINDER_CANCAL, flow_code, info);
				} else
					operateView.initViewData(OperateView.DialogEnum.DOUBLE_REMINDER_BACK, flow_code, info);
				operateView.setPopWindowLongClickListener(OrderQueryAdapter.this);
				if (isRefreshData)
					notifyDataSetChanged();
			} else
			{
				restoreCurrentView(operateView);
			}
		} else if (info.getGoods_type() == VodConstant.BUNDLE_TYPE)
		{
			//不属于活动的套餐
			if (operateView.getVisibility() == View.GONE)
			{
				currentView = operateView;
				goodsInfo.setGoodsId(info.getGoodsId());
				goodsInfo.setD_id_value(info.getD_id_value());
				goodsInfo.setShowOperate(true);
				goodsInfo.setParentDIdValue(parentDidValue);
				operateView.setVisibility(View.VISIBLE);
				if (info.is_give() && TextUtils.isEmpty(info.getBill_promotion_id()))
				{
					operateView.initViewData(OperateView.DialogEnum.SINGLE_CANCAL, flow_code, info);
				} else if (!info.is_give())
				{
					operateView.initViewData(OperateView.DialogEnum.SINGLE_BACK, flow_code, info);
				}
				operateView.setPopWindowLongClickListener(OrderQueryAdapter.this);
				if (isRefreshData)
					notifyDataSetChanged();
			} else
			{
				restoreCurrentView(operateView);
			}
		} else
		{
			if (operateView.getVisibility() == View.GONE)
			{
				currentView = operateView;
				goodsInfo.setGoodsId(info.getGoodsId());
				goodsInfo.setD_id_value(info.getD_id_value());
				goodsInfo.setShowOperate(true);
				goodsInfo.setParentDIdValue(parentDidValue);
				operateView.setVisibility(View.VISIBLE);
				if (info.is_give())//活动
					operateView.initViewData(OperateView.DialogEnum.SINGLE_CANCAL, flow_code, info);
				else
					operateView.initViewData(OperateView.DialogEnum.SINGLE_BACK, flow_code, info);
				operateView.setPopWindowLongClickListener(OrderQueryAdapter.this);
				if (isRefreshData)
					notifyDataSetChanged();
			} else
			{
				restoreCurrentView(operateView);
			}
		}
	}

	/**
	 * 还原当前操作行数据
	 * @param operateView
	 */
	private void restoreCurrentView(OperateView operateView)
	{
		currentView = null;
		operateView.setVisibility(View.GONE);
		goodsInfo.setD_id_value("");
		goodsInfo.setGoodsId("");
		goodsInfo.setParentDIdValue("");
		goodsInfo.setShowOperate(false);
	}

	/**
	 * 隐藏操作行
	 */
	public void hideOperateView()
	{
		if (currentView != null)
		{
			currentView.setVisibility(View.GONE);
			goodsInfo.setD_id_value("");
			goodsInfo.setGoodsId("");
			goodsInfo.setParentDIdValue("");
			goodsInfo.setShowOperate(false);
		}
	}

	@Override
	public void cancelGive(Object object, String flow_code)
	{
		hideOperateView();
		GoodsInfo info = (GoodsInfo) object;
		Intent intent = new Intent(context, AuthorizationDialogActivity.class);
		intent.putExtra(IntentKey.GOODSINFO, info);
		intent.putExtra(IntentKey.FLOW_CODE, flow_code);
		intent.putExtra(IntentKey.TOKEN_EVENT, "门店POS系统>点餐系统模块>点餐>取消赠送");//传入接口事件名
		intent.putExtra(IntentKey.INTERFACE_NAME, ApiConfig.VOD_TOKEN);//传入接口名字
		alreadyOrderFragment.startActivityForResult(intent, IntentCode.ORDER_SEARCH_REQUEST_GIVE);
	}

	@Override
	public void back(Object object, String flow_code)
	{
		hideOperateView();
		GoodsInfo info = (GoodsInfo) object;
		int num = getAlreadyBackNum(info, flow_code);
		if (info.getOrderNum() + num <= 0)
		{
			ToastUtils.showText(context, "该商品已退单");
			return;
		}
		Intent intent = new Intent(context, CustomDialogActivity.class);
		intent.putExtra(IntentKey.GOODSINFO, info);
		intent.putExtra(IntentKey.FLOW_CODE, flow_code);
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, info.getGoodsName());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, String.valueOf(info.getOrderNum() + num));
		intent.putExtra(IntentKey.UNIT, info.getUnit());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "退单数量");
		intent.putExtra(IntentKey.MAX_BACK_NUMBER, info.getOrderNum() + num);
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TYPE, CustomDialogActivity.DialogEnum.CUSTOM_VIEW_BACK.toString());
		intent.putExtra(IntentKey.OUTPUT_STATUS, info.getOutput_status());
		alreadyOrderFragment.startActivityForResult(intent, IntentCode.ORDER_SEARCH_REQUEST_BACK);
	}

	@Override
	public void reminder(Object object, String flow_code)
	{
		hideOperateView();
		if (object instanceof GoodsInfo)
		{
			GoodsInfo goodsInfo = (GoodsInfo) object;
			int num = getAlreadyBackNum(goodsInfo, flow_code);
			if (goodsInfo.getOutput_status() == 2)
				ToastUtils.showText(context, "商品已划单");
			else if (goodsInfo.getOrderNum() + num <= 0)
			{
				ToastUtils.showText(context, "商品已退单");
			} else
			{
				reminder(((GoodsInfo) object).getGoodsName(), ((GoodsInfo) object).getD_id_key(), ((GoodsInfo) object).getD_id_value());
			}
		} else if (object instanceof FoodInfo)
		{
			if (((FoodInfo) object).getOutput_status() == 2)
				ToastUtils.showText(context, "商品已划单");
			else
				reminder(((FoodInfo) object).getName(), ((FoodInfo) object).getD_id_key(), ((FoodInfo) object).getD_id_value());
		} else
		{
			if (((FoodDetailInfo) object).getOutput_status() == 2)
				ToastUtils.showText(context, "商品已划单");
			else
				reminder(((FoodDetailInfo) object).getName(), ((FoodDetailInfo) object).getD_id_key(), ((FoodDetailInfo) object).getD_id_value());
		}
	}

	@Override
	public void change(Object obj, String flow_code)
	{
		hideOperateView();
		Intent intent = new Intent(context, OrderChangeActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, "更换商品");
		intent.putExtra(IntentKey.ROOM_ID, roomId);
		intent.putExtra(IntentKey.BILL_ID, billId);
		intent.putExtra(IntentKey.CHARGING_ROOM_TYPE_ID, charging_room_type_id);
		intent.putExtra(IntentKey.GOODSINFO, (FoodDetailInfo) obj);
		alreadyOrderFragment.startActivityForResult(intent, IntentCode.ORDER_SEARCH_REQUEST_CHANGE);
	}

	/**
	 * 催单
	 *
	 * @param name 商品名字
	 */
	private void reminder(String name, final String d_id_key, final String d_id_value)
	{
		hideOperateView();
		new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure).cancelable(false)
				.contentName("确定要对" + name + "催单吗？").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
				{
					@Override
					public void onOnlyCancelClick()
					{

					}

					@Override
					public void onOkClick()
					{
						HashMap<String, String> map = new HashMap<String, String>();
						map.put(d_id_key, d_id_value);
						HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.ATTENTION).param(map)
								.httpMethodEnum(HttpMethodEnum.POST).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_NONE)
								.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
								{

									@Override
									public void onResponseSucess(JSONObject jsonObject)
									{
										ToastUtils.showText(context, "催单成功");
									}
								}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
								{

									@Override
									public void onError(int errno, String errMsg)
									{
										ToastUtils.showText(context, "催单失败");
									}
								}).build();
						httpRequestTask.execute();
					}

					@Override
					public void onCancelClick()
					{

					}
				}).build().show();
	}

	protected class ViewHolder
	{
		protected BaseTextView tv_title;
		protected BaseTextView tv_time;
		protected LinearLayout lay_content_div;
	}

	/**
	 * 套餐或者活动是否更换过商品
	 *
	 * @param flow_code
	 * @return
	 */
	private boolean isChangeGoods(GoodsInfo goodsInfo, String flow_code)
	{
		int num = goodsInfo.getOrderNum() + getAlreadyBackNum(goodsInfo, flow_code);
		if (TextUtils.equals(goodsInfo.getD_id_key(), "bill_bundle_id") || TextUtils.equals(goodsInfo.getD_id_key(), "bill_goods_active_id"))
		{
			if (num <= 0)
				return true;
			return false;
		} else
		{
			return true;
		}
	}

	private void parseGoodInfo()
	{
		for (int i = 0; i < infoList.size(); i++)
		{
			OrderInfo info = infoList.get(i);
			info.setGoodsInfoList(parseGoodInfo(info));
		}
	}

	/**
	 * 解析点餐商品
	 */
	private List<GoodsInfo> parseGoodInfo(OrderInfo info)
	{
		List<GoodsInfo> allList = new ArrayList<GoodsInfo>();
		List<GoodsInfo> retails = new ArrayList<GoodsInfo>();
		List<GoodsInfo> bundles = new ArrayList<GoodsInfo>();
		List<GoodsInfo> actives = new ArrayList<GoodsInfo>();
		List<GoodsInfo> backGoods = new ArrayList<GoodsInfo>();
		try
		{
			if (!TextUtils.isEmpty(info.getRetails()))
			{
				JSONArray array = new JSONArray(info.getRetails());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoods_type(0);
						JSONObject object = array.getJSONObject(i);
						goodsInfo.setGoodsId(object.optString("id"));
						goodsInfo.setIs_give(!(object.optInt("is_present") == 0));
						goodsInfo.setGoodsName(object.optString("name"));
						goodsInfo.setOutput_status(object.optInt("output_status"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setOrderNum(object.optInt("quantity"));
						goodsInfo.setUnit(object.optString("unit"));
						goodsInfo.setD_id_key(object.optString("d_id_key"));
						goodsInfo.setD_id_value(object.optString("d_id_value"));
						goodsInfo.setParentDIdValue(object.optString("d_id_value"));
						goodsInfo.setBill_promotion_id(object.optString("bill_promotion_id"));
						retails.add(goodsInfo);
					}
				}
			}

			if (!TextUtils.isEmpty(info.getBundles()))
			{
				JSONArray array = new JSONArray(info.getBundles());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoods_type(1);
						JSONObject object = array.getJSONObject(i);
						goodsInfo.setGoodsId(object.optString("id"));
						goodsInfo.setIs_give(!(object.optInt("is_present") == 0));
						goodsInfo.setGoodsName(object.optString("name"));
						goodsInfo.setIs_outposition(object.optInt("have_meal_device"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setOrderNum(object.optInt("quantity"));
						goodsInfo.setOutput_status(object.optInt("output_status"));
						goodsInfo.setUnit(object.optString("unit"));
						goodsInfo.setD_id_key(object.optString("d_id_key"));
						goodsInfo.setD_id_value(object.optString("d_id_value"));
						goodsInfo.setParentDIdValue(object.optString("d_id_value"));
						goodsInfo.setBill_promotion_id(object.optString("bill_promotion_id"));
						JSONArray retailsArray = object.optJSONArray("retails");
						if (retailsArray != null && retailsArray.length() > 0)
						{
							goodsInfo.setRetails(retailsArray.toString());
							goodsInfo.setFoodDetailInfoList(parseFoodDetailInfo(goodsInfo));
						}
						bundles.add(goodsInfo);
					}
				}
			}

			if (!TextUtils.isEmpty(info.getActives()))
			{
				JSONArray array = new JSONArray(info.getActives());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						GoodsInfo goodsInfo = new GoodsInfo();
						JSONObject object = array.getJSONObject(i);
						goodsInfo.setGoods_type(2);
						goodsInfo.setGoodsId(object.optString("id"));
						goodsInfo.setIs_give(!(object.optInt("is_present") == 0));
						goodsInfo.setGoodsName(object.optString("name"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setOrderNum(object.optInt("quantity"));
						goodsInfo.setUnit(object.optString("unit"));
						goodsInfo.setDecrease_time(object.optInt("free_time"));
						goodsInfo.setD_id_key(object.optString("d_id_key"));
						goodsInfo.setD_id_value(object.optString("d_id_value"));
						goodsInfo.setParentDIdValue(object.optString("d_id_value"));
						JSONArray retailsArray = object.optJSONArray("retails");
						if (retailsArray != null && retailsArray.length() > 0)
						{
							goodsInfo.setRetails(retailsArray.toString());
							goodsInfo.setFoodDetailInfoList(parseFoodDetailInfo(goodsInfo));
						}
						JSONArray bundlesArray = object.optJSONArray("bundles");
						if (bundlesArray != null && bundlesArray.length() > 0)
						{
							goodsInfo.setBundles(bundlesArray.toString());
							goodsInfo.setFoodInfoList(parseFoodInfo(goodsInfo));
						}
						actives.add(goodsInfo);
					}
				}
			}

			if (!TextUtils.isEmpty(info.getBackgoods()))
			{
				JSONObject jsonObject = new JSONObject(info.getBackgoods());
				JSONArray retailsArray = jsonObject.optJSONArray("retails");
				if (retailsArray != null && retailsArray.length() > 0)
				{
					for (int j = 0; j < retailsArray.length(); j++)
					{
						JSONObject obj = retailsArray.getJSONObject(j);
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoodsId(obj.optString("id"));
						goodsInfo.setGoodsName(obj.optString("name"));
						goodsInfo.setBackNum(obj.optInt("quantity"));
						goodsInfo.setUnit(obj.optString("unit"));
						goodsInfo.setPrice(obj.optInt("price"));
						goodsInfo.setIs_back(true);
						backGoods.add(goodsInfo);
					}
				}
				JSONArray bundlesArray = jsonObject.optJSONArray("bundles");
				if (bundlesArray != null && bundlesArray.length() > 0)
				{
					for (int j = 0; j < bundlesArray.length(); j++)
					{
						JSONObject obj = bundlesArray.getJSONObject(j);
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoodsId(obj.optString("id"));
						goodsInfo.setGoodsName(obj.optString("name"));
						goodsInfo.setBackNum(obj.optInt("quantity"));
						goodsInfo.setUnit(obj.optString("unit"));
						goodsInfo.setPrice(obj.optInt("price"));
						goodsInfo.setIs_back(true);
						backGoods.add(goodsInfo);
					}
				}
				JSONArray activesArray = jsonObject.optJSONArray("actives");
				if (activesArray != null && activesArray.length() > 0)
				{
					for (int j = 0; j < activesArray.length(); j++)
					{
						JSONObject obj = activesArray.getJSONObject(j);
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoodsId(obj.optString("id"));
						goodsInfo.setGoodsName(obj.optString("name"));
						goodsInfo.setBackNum(obj.optInt("quantity"));
						goodsInfo.setUnit(obj.optString("unit"));
						goodsInfo.setPrice(obj.optInt("price"));
						goodsInfo.setIs_back(true);
						backGoods.add(goodsInfo);
					}
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		if (!CollectionUtils.isEmpty(retails))
		{
			allList.addAll(retails);
		}
		if (!CollectionUtils.isEmpty(bundles))
		{
			allList.addAll(bundles);
		}
		if (!CollectionUtils.isEmpty(actives))
		{
			allList.addAll(actives);
		}
		if (!CollectionUtils.isEmpty(backGoods))
		{
			allList.addAll(backGoods);
		}
		return allList;
	}

	/**
	 * 取消增送--对比刷新数据
	 */
	public void cancelGiveDataForRefresh(GoodsInfo goodsInfo, String flow_code)
	{
		if (null != goodsInfo)
		{
			String goodsId = goodsInfo.getD_id_value();
			int goodsType = goodsInfo.getGoods_type();
			try
			{
				for (OrderInfo info : infoList)
				{
					if (info.getFlow_code().equals(flow_code))
					{
						if (goodsType == VodConstant.RETAIL_TYPE)
						{
							if (!TextUtils.isEmpty(info.getRetails()))
							{
								JSONArray array = new JSONArray(info.getRetails());
								JSONArray newArray = new JSONArray();
								if (array.length() > 0)
								{
									for (int i = 0; i < array.length(); i++)
									{
										JSONObject object = array.getJSONObject(i);
										if (goodsId.equals(object.optString("d_id_value")) && object.optInt("is_present") == 1)
										{
											object.put("is_present", 0);
											newArray.put(object);
										} else
											newArray.put(object);
									}
									info.setRetails(newArray.toString());
								}
							}
						} else if (goodsType == VodConstant.BUNDLE_TYPE)
						{
							if (!TextUtils.isEmpty(info.getBundles()))
							{
								JSONArray array = new JSONArray(info.getBundles());
								JSONArray newArray = new JSONArray();
								if (array.length() > 0)
								{
									for (int i = 0; i < array.length(); i++)
									{
										JSONObject object = array.getJSONObject(i);
										if (goodsId.equals(object.optString("d_id_value")) && object.optInt("is_present") == 1)
										{
											object.put("is_present", 0);
											newArray.put(object);
										} else
											newArray.put(object);
									}
									info.setBundles(newArray.toString());
								}
							}
						} else
						{
							if (!TextUtils.isEmpty(info.getActives()))
							{
								JSONArray array = new JSONArray(info.getActives());
								JSONArray newArray = new JSONArray();
								if (array.length() > 0)
								{
									for (int i = 0; i < array.length(); i++)
									{
										JSONObject object = array.getJSONObject(i);
										if (goodsId.equals(object.optString("d_id_value")) && object.optInt("is_present") == 1)
										{
											object.put("is_present", 0);
											newArray.put(object);
										} else
											newArray.put(object);
									}
									info.setActives(newArray.toString());
								}
							}
						}
					}
				}
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			parseGoodInfo();
			this.notifyDataSetChanged();
		}
	}

	/**
	 * 退单刷新数据
	 *
	 * @param goodsInfo 商品信息
	 * @param flow_code 流水号
	 */
	public void backDataForRefresh(GoodsInfo goodsInfo, String flow_code)
	{
		if (null == goodsInfo)
			return;
		int goodsType = goodsInfo.getGoods_type();
		try
		{
			for (OrderInfo info : infoList)
			{
				if (info.getFlow_code().equals(flow_code))
				{
					JSONObject obj = new JSONObject();
					if (goodsType == VodConstant.RETAIL_TYPE)
					{
						obj.put("id", goodsInfo.getGoodsId());
						obj.put("name", goodsInfo.getGoodsName());
						obj.put("quantity", -goodsInfo.getBackNum());
						obj.put("cancel_id", goodsInfo.getD_id_value());
						obj.put("unit", goodsInfo.getUnit());
						obj.put("price", goodsInfo.getPrice());
						if (TextUtils.isEmpty(info.getBackgoods()))
						{
							JSONObject object = new JSONObject();
							JSONArray array = new JSONArray();
							array.put(obj);
							object.put("retails", array);
							info.setBackgoods(object.toString());
						} else
						{
							JSONObject backGoods = new JSONObject();
							JSONObject object = new JSONObject(info.getBackgoods());
							JSONArray retails = object.optJSONArray("retails");
							JSONArray bundles = object.optJSONArray("bundles");
							JSONArray actives = object.optJSONArray("actives");
							if (retails != null && retails.length() > 0)
							{
								boolean isExit = false;
								for (int i = 0; i < retails.length(); i++)
								{
									JSONObject object1 = retails.optJSONObject(i);
									if (goodsInfo.getD_id_value().equals(object1.optString("cancel_id")))
									{
										isExit = true;
										object1.put("quantity", -goodsInfo.getBackNum() + object1.optInt("quantity"));
										break;
									}
								}
								if (!isExit)
									retails.put(obj);
							} else
							{
								retails = new JSONArray();
								retails.put(obj);
							}
							backGoods.put("retails", retails);
							if (bundles != null)
								backGoods.put("bundles", bundles);
							if (actives != null)
								backGoods.put("actives", actives);
							info.setBackgoods(backGoods.toString());
						}
						break;
					} else if (goodsType == VodConstant.BUNDLE_TYPE)
					{
						obj.put("id", goodsInfo.getGoodsId());
						obj.put("name", goodsInfo.getGoodsName());
						obj.put("quantity", -goodsInfo.getBackNum());
						obj.put("cancel_id", goodsInfo.getD_id_value());
						obj.put("unit", goodsInfo.getUnit());
						obj.put("price", goodsInfo.getPrice());
						if (TextUtils.isEmpty(info.getBackgoods()))
						{
							JSONObject object = new JSONObject();
							JSONArray array = new JSONArray();
							array.put(obj);
							object.put("bundles", array);
							info.setBackgoods(object.toString());
						} else
						{
							JSONObject backGoods = new JSONObject();
							JSONObject object = new JSONObject(info.getBackgoods());
							JSONArray retails = object.optJSONArray("retails");
							JSONArray bundles = object.optJSONArray("bundles");
							JSONArray actives = object.optJSONArray("actives");
							if (bundles != null && bundles.length() > 0)
							{
								boolean isExit = false;
								for (int i = 0; i < bundles.length(); i++)
								{
									JSONObject object1 = bundles.optJSONObject(i);
									if (goodsInfo.getD_id_value().equals(object1.optString("cancel_id")))
									{
										isExit = true;
										object1.put("quantity", -goodsInfo.getBackNum() + object1.optInt("quantity"));
										break;
									}
								}
								if (!isExit)
									bundles.put(obj);
							} else
							{
								bundles = new JSONArray();
								bundles.put(obj);
							}
							if (retails != null)
								backGoods.put("retails", retails);
							backGoods.put("bundles", bundles);
							if (actives != null)
								backGoods.put("actives", actives);
							info.setBackgoods(backGoods.toString());
						}
						break;
					} else if (goodsType == VodConstant.ACTIVE_TYPE)
					{
						obj.put("id", goodsInfo.getGoodsId());
						obj.put("name", goodsInfo.getGoodsName());
						obj.put("quantity", -goodsInfo.getBackNum());
						obj.put("cancel_id", goodsInfo.getD_id_value());
						obj.put("unit", goodsInfo.getUnit());
						obj.put("price", goodsInfo.getPrice());
						if (TextUtils.isEmpty(info.getBackgoods()))
						{
							JSONObject object = new JSONObject();
							JSONArray array = new JSONArray();
							array.put(obj);
							object.put("actives", array);
							info.setBackgoods(object.toString());
						} else
						{
							JSONObject backGoods = new JSONObject();
							JSONObject object = new JSONObject(info.getBackgoods());
							JSONArray retails = object.optJSONArray("retails");
							JSONArray bundles = object.optJSONArray("bundles");
							JSONArray actives = object.optJSONArray("actives");
							if (actives != null && actives.length() > 0)
							{
								boolean isExit = false;
								for (int i = 0; i < actives.length(); i++)
								{
									JSONObject object1 = actives.optJSONObject(i);
									if (goodsInfo.getD_id_value().equals(object1.optString("cancel_id")))
									{
										isExit = true;
										object1.put("quantity", -goodsInfo.getBackNum() + object1.optInt("quantity"));
										break;
									}
								}
								if (!isExit)
									actives.put(obj);
							} else
							{
								actives = new JSONArray();
								actives.put(obj);
							}
							if (retails != null)
								backGoods.put("retails", retails);
							if (bundles != null)
								backGoods.put("bundles", bundles);
							backGoods.put("actives", actives);
							info.setBackgoods(backGoods.toString());
						}
						break;
					}
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		parseGoodInfo();
		this.notifyDataSetChanged();
	}

	/**
	 * 解析套餐详情里面的商品
	 *
	 * @param info 商品信息
	 * @return 套餐信息列表
	 */
	private List<FoodDetailInfo> parseFoodDetailInfo(GoodsInfo info)
	{
		List<FoodDetailInfo> retails = new ArrayList<FoodDetailInfo>();
		try
		{
			if (!TextUtils.isEmpty(info.getRetails()))
			{
				JSONArray array = new JSONArray(info.getRetails());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						FoodDetailInfo foodDetailInfo = new FoodDetailInfo();
						JSONObject object = array.getJSONObject(i);
						foodDetailInfo.setFood_id(object.optString("id"));
						foodDetailInfo.setPrice(object.optInt("price"));
						foodDetailInfo.setName(object.optString("name"));
						foodDetailInfo.setOperateTypeId(object.optString("operate_type_id"));
						foodDetailInfo.setOutput_status(object.optInt("output_status"));
						foodDetailInfo.setQuantity(object.optInt("quantity"));
						foodDetailInfo.setD_id_key(object.optString("d_id_key"));
						foodDetailInfo.setD_id_value(object.optString("d_id_value"));
						foodDetailInfo.setUnit(object.optString("unit"));
						retails.add(foodDetailInfo);
					}
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		return retails;
	}

	/**
	 * 解析活动详情里面的套餐或者商品
	 *
	 * @param info 商品信息
	 * @return 套餐或者商品
	 */
	private List<FoodInfo> parseFoodInfo(GoodsInfo info)
	{
		List<FoodInfo> bundles = new ArrayList<FoodInfo>();
		try
		{
			if (!TextUtils.isEmpty(info.getBundles()))
			{
				JSONArray array = new JSONArray(info.getBundles());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						FoodInfo foodInfo = new FoodInfo();
						JSONObject object = array.getJSONObject(i);
						foodInfo.setId(object.optString("id"));
						foodInfo.setName(object.optString("name"));
						foodInfo.setQuantity(object.optInt("quantity"));
						foodInfo.setUnit(object.optString("unit"));
						foodInfo.setIs_outposition(object.optInt("have_meal_device"));
						foodInfo.setOutput_status(object.optInt("output_status"));
						foodInfo.setD_id_key(object.optString("d_id_key"));
						foodInfo.setD_id_value(object.optString("d_id_value"));
						JSONArray retailsArray = object.optJSONArray("retails");
						if (retailsArray != null && retailsArray.length() > 0)
						{
							foodInfo.setGoods(retailsArray.toString());
							foodInfo.setFoodList(parseFoodDetailInfo(foodInfo));
						}
						bundles.add(foodInfo);
					}
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		return bundles;
	}

	/**
	 * 解析活动详情里面订单套餐详情里面的商品
	 *
	 * @param info 套餐信息
	 * @return 套餐详情列表
	 */
	private List<FoodDetailInfo> parseFoodDetailInfo(FoodInfo info)
	{
		List<FoodDetailInfo> retails = new ArrayList<FoodDetailInfo>();
		try
		{
			if (!TextUtils.isEmpty(info.getGoods()))
			{
				JSONArray array = new JSONArray(info.getGoods());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						FoodDetailInfo foodDetailInfo = new FoodDetailInfo();
						JSONObject object = array.getJSONObject(i);
						foodDetailInfo.setFood_id(object.optString("id"));
						foodDetailInfo.setName(object.optString("name"));
						foodDetailInfo.setPrice(object.optInt("price"));
						foodDetailInfo.setOperateTypeId(object.optString("operate_type_id"));
						foodDetailInfo.setOutput_status(object.optInt("output_status"));
						foodDetailInfo.setD_id_key(object.optString("d_id_key"));
						foodDetailInfo.setD_id_value(object.optString("d_id_value"));
						foodDetailInfo.setQuantity(object.optInt("quantity"));
						foodDetailInfo.setUnit(object.optString("unit"));
						retails.add(foodDetailInfo);
					}
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		return retails;
	}

	/**
	 * 获取已退单的数量
	 *
	 * @return 退单数量
	 */
	public int getAlreadyBackNum(GoodsInfo goodsInfo, String flow_code)
	{
		int num = 0;
		if (null == goodsInfo)
			return num;
		int goodsType = goodsInfo.getGoods_type();
		try
		{
			for (OrderInfo info : infoList)
			{
				if (info.getFlow_code().equals(flow_code))
				{
					if (TextUtils.isEmpty(info.getBackgoods()))
						return num;
					if (goodsType == VodConstant.RETAIL_TYPE)
					{
						JSONObject object = new JSONObject(info.getBackgoods());
						JSONArray retails = object.optJSONArray("retails");
						if (retails == null)
							return num;
						else
						{
							for (int i = 0; i < retails.length(); i++)
							{
								JSONObject object1 = retails.getJSONObject(i);
								if (goodsInfo.getD_id_value().equals(object1.optString("cancel_id")))
								{
									num += object1.optInt("quantity");
								}
							}
						}
						break;
					} else if (goodsType == VodConstant.BUNDLE_TYPE)
					{
						JSONObject object = new JSONObject(info.getBackgoods());
						JSONArray bundles = object.optJSONArray("bundles");
						if (bundles == null)
							return num;
						else
						{
							for (int i = 0; i < bundles.length(); i++)
							{
								JSONObject object1 = bundles.getJSONObject(i);
								if (goodsInfo.getD_id_value().equals(object1.optString("cancel_id")))
								{
									num += object1.optInt("quantity");
								}
							}
						}
						break;
					} else if (goodsType == VodConstant.ACTIVE_TYPE)
					{
						JSONObject object = new JSONObject(info.getBackgoods());
						JSONArray actives = object.optJSONArray("actives");
						if (actives == null)
							return num;
						else
						{
							for (int i = 0; i < actives.length(); i++)
							{
								JSONObject object1 = actives.getJSONObject(i);
								if (goodsInfo.getD_id_value().equals(object1.optString("cancel_id")))
								{
									num += object1.optInt("quantity");
								}
							}
						}
						break;
					}
				}
				break;
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		return num;
	}
}
